package com.shopcart.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopcart.mapper.ShoppingCartMapper;
import com.shopcart.pojo.ShoppingCart;
import com.shopcart.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private  ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCart addToCart(ShoppingCart shoppingCart) {
        shoppingCartMapper.insert(shoppingCart);
        return shoppingCart;
    }

    @Override
    public ShoppingCart updateCartItem(ShoppingCart shoppingCart) {
        shoppingCartMapper.updateById(shoppingCart);
        return shoppingCart;
    }

    @Override
    public void deleteCartItem(String cartId) {
        shoppingCartMapper.deleteById(cartId);
    }

    @Override
    public List<ShoppingCart> listCartItemsByUserId(String userId) {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return shoppingCartMapper.selectList(queryWrapper);
    }

    @Override
    public ShoppingCart getCartItemById(String cartId) {
        return shoppingCartMapper.selectById(cartId);
    }
}